import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const planUrl = CommonConfig.planningUrl + '/v1'
const schemaUrl = CommonConfig.schemaUrl + '/v1'
import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
/**
 * 获取车间列表
 * plantId 分厂id（空为总厂）
 **/
export function getWorkshopList () {
  let reqData = {
    params: {
      plantId: ''
    }
  }
  const newUrl = schemaUrl + '/workshop_kv'
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 根据车间 ID 获取工位列表
 * workshopId 车间id
 **/
export function getWorkstationList (query) {
  let reqData = {
    params: {
      workshopId: query.workshopId || undefined
    }
  }
  const newUrl = schemaUrl + `/workshops/${query.workshopId}/work_centers`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取设备类型
 * dicGroupCode: machine_type
 * disable: true
 **/
export function getDeviceTypes () {
  let reqData = {
    params: {
      dicGroupCode: 'machine_type',
      disable: true
    }
  }
  const newUrl = schemaUrl + '/dict_by_gcode'
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取轮班列表
 * id：车间ID
 **/
export function getShiftsList (query) {
  let reqData = {
    params: {
      id: query.id || undefined
    }
  }
  const newUrl = schemaUrl + `/workshop/${query.id}/shifts/use`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 获取当前班次
 * id：车间ID
 * schema/v1/workshop/19/shifts:use/id
 **/
export function getNowShift (workshopId) {
  const newUrl = schemaUrl + `/workshop/${workshopId}/shifts:use/id`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 获取轮班列表
 * 12-30 根据工单获取班组列表
 *
/v1/work_orders/{workOrderId}/checkin_shifts
根据工单ID 获取报工班制列表
 **/
export function getCheckinShifts (workOrderId) {
  const newUrl = planUrl + `/work_orders/${workOrderId}/checkin_shifts`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取人机绑定列表
 * id：车间ID
 * workCenterIds: 工位id
 * deviceType: 设备类型
 * pageNum：页码（从 1 起计）
 * pageSize：每页条数
 **/
export function getBindingList (query) {
  let reqData = {
    params: {
      isPersonalComputer: true,
      deviceType: query.deviceType || undefined,
      // pageNum: query.pageNum || undefined,
      // pageSize: query.pageSize || undefined,
      workCenterIds: query.workCenterIds || undefined,
      searchBy: query.searchBy || undefined,
      keyword: query.keyword || undefined,
      schemeId: query.schemeId,
      level: query.level
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  const newUrl = planUrl + `/workshops/${query.id}/user_device_binding`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 批量绑定
 * workshopId：车间ID
 * userType: 操作员/技术员 【operator/technician】
 * userId: 用户ID
 * shiftIds：班制ID List
 * deviceIds：设备ID List
 **/
export function batchBind (query) {
  const params = {
    deviceIds: query.deviceIds || undefined,
    shiftIds: query.shiftIds || undefined,
    userId: query.userId || undefined,
    userType: query.userType || undefined,
    workshopId: query.workshopId || undefined
  }
  const newUrl = planUrl + '/user_device_binding:batchBind'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 批量解绑
 * userType: 操作员/技术员 【operator/technician】
 * shiftIds：班制ID List
 * deviceIds：设备ID List
 **/
export function batchUnbind (query) {
  const params = {
    deviceIds: query.deviceIds || undefined,
    shiftIds: query.shiftIds || undefined,
    userType: query.userType || undefined
  }
  const newUrl = planUrl + '/user_device_binding:batchUnbind'
  return axiosInstance.patch(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 单人解绑
 * userType: 操作员/技术员 【operator/technician】
 * shiftId：班制ID List
 * deviceId：设备ID List
 * userId: 用户ID
 **/
export function singleUnbind (query) {
  const params = {
    deviceId: query.deviceId || undefined,
    shiftId: query.shiftId || undefined,
    userType: query.userType || undefined,
    userId: query.userId || undefined
  }
  const newUrl = planUrl + '/user_device_binding:unbind'
  return axiosInstance.patch(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 单人解绑
 * userType: 操作员/技术员 【operator/technician】
 * shiftId：班制ID
 * deviceId：设备ID
 * userId: 用户ID
 * workshopId: 车间ID
 **/
export function singleBind (query) {
  const params = {
    deviceId: query.deviceId || undefined,
    shiftId: query.shiftId || undefined,
    userType: query.userType || undefined,
    userId: query.userId || undefined,
    workshopId: query.workshopId || undefined
  }
  const newUrl = planUrl + '/user_device_binding:bind'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 获取绑定历史
 * pageNum: 页码（从 1 起计）
 * pageSize：班制ID List
 * deviceIds：每页条数
 **/
export function getBindHistory (query) {
  let config = {
    params: {
      pageNum: query.pageNum || undefined,
      shiftsIds: query.shiftsIds || undefined,
      pageSize: query.pageSize || undefined,
      deviceId: query.deviceId
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const newUrl = planUrl + '/user_device_binding/logs:batchGet'
  return axiosInstance.get(newUrl, config).then((res) => {
    return Promise.resolve((res))
  })
}
